package net.starwifi.duobao.utils;

import java.security.MessageDigest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Utils-String.
 *
 * @author <a href="https://ruoo.whosenet.com">ruo.whosenet.com</>
 * @version 1.0
 */
public class StringUtils {

    public static String getUUID(){
        return UUID.randomUUID().toString().replace("-","").toUpperCase();
    }



    public static String toMD5(String key){
        MessageDigest code;
        StringBuffer sb=new StringBuffer();
        try {
            code = MessageDigest.getInstance("MD5");
            code.update((key).getBytes());
            byte[] bs=code.digest();
            for(int i=0;i<bs.length;i++){
                int v=bs[i]&0xFF;
                if(v<16){
                    sb.append(0);
                }
                sb.append(Integer.toHexString(v).toUpperCase());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static String toMD5(byte[] key){
        MessageDigest code;
        StringBuffer sb=new StringBuffer();
        try {
            code = MessageDigest.getInstance("MD5");
            code.update(key);
            byte[] bs=code.digest();
            for(int i=0;i<bs.length;i++){
                int v=bs[i]&0xFF;
                if(v<16){
                    sb.append(0);
                }
                sb.append(Integer.toHexString(v).toUpperCase());
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return sb.toString();
    }



    public static boolean isMobileNO(String mobile){
        String regExp = "^[1]([3][0-2]{1}|55|56|85|86)[0-9]{8}$";
        Pattern p = Pattern.compile(regExp);
        Matcher m = p.matcher(mobile);
        return m.find();
    }

    public static String formatDate(Date date){
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(date);
    }

    public static String formatDate(Date date, String fStr) {
        DateFormat format = new SimpleDateFormat(fStr);
        return format.format(date);
    }


    public static String getTimeStamp(){
        DateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date());
    }


    /** 生成数字验证码*/
    public static String getRandomNumber(int len){
        String content="";
        for(int i=0;i<len;i++){
            content=content+ new Random().nextInt(10);
        }
        return content;
    }




}
